﻿using System.Windows;
using System.Windows.Controls;
using SmartSpace.Workflow;
using SsTool.Workflow.Windows;

namespace SsTool.Workflow
{
    /// <summary>
    /// Interaction logic for WorkflowMetadataListPage.xaml
    /// </summary>
    public partial class WorkflowMetadataListPage : Page
    {
        public WorkflowMetadataListPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(WorkflowMetadataListPage_Loaded);
        }

        void WorkflowMetadataListPage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadDatas();
        }

        private void LoadDatas()
        {
            IWorkflowService service = App.Get<IWorkflowService>();
            _ctlGridWorkflow.ItemsSource = service.FindWorkflows(0, 999);
        }

        private void _ctlBtnCreate_Click(object sender, RoutedEventArgs e)
        {
            CreateWorkflowMetadataWindow window = new CreateWorkflowMetadataWindow();
            if (window.ShowDialog() == true)
            {
                LoadDatas();
            }
        }

        private void _ctlBtnUpdate_Click(object sender, RoutedEventArgs e)
        {
            WorkflowMetadata selectedItem = _ctlGridWorkflow.SelectedItem as WorkflowMetadata;
            if (selectedItem != null)
            {
                WorkflowMetadata wfMetadata = App.Get<IWorkflowService>().FindWorkflow(selectedItem.Id);
                CreateWorkflowMetadataWindow window = new CreateWorkflowMetadataWindow(wfMetadata);
                if (window.ShowDialog() == true)
                {
                    LoadDatas();
                }
            }
        }
    }
}
